package com.shupian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.AddTagDto;
import com.shupian.domain.dto.TagListDto;
import com.shupian.domain.entity.Tag;
import com.shupian.domain.vo.PageVo;
import com.shupian.domain.vo.TagListVo;
import com.shupian.domain.vo.TagPageVo;
import com.shupian.domain.vo.TagVo;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.mapper.TagMapper;
import com.shupian.service.TagService;
import com.shupian.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-08-28 09:21:25
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);
        //分装数据返回
        List<TagPageVo> tagPageVos = BeanCopyUtils.copyBeanList(page.getRecords(), TagPageVo.class);
        PageVo pageVo = new PageVo(tagPageVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(AddTagDto tagDto) {
        Tag tag = new Tag(tagDto.getName(),tagDto.getRemark());
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTag(Long id) {
        if (removeById(id)) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.VALUE_ERROR);
    }

    @Override
    public ResponseResult getTagById(Long id) {
        Tag tag = getById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult updateTag(TagVo tagVo) {
        Tag tag = BeanCopyUtils.copyBean(tagVo, Tag.class);
        updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getAllTag() {
        List<Tag> tags = list();
        List<TagListVo> tagListVos = BeanCopyUtils.copyBeanList(tags, TagListVo.class);
        return ResponseResult.okResult(tagListVos);
    }
}

